package stu.aistar.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.lettuce.core.ScriptOutputType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import stu.aistar.mapper.UserMapper;
import stu.aistar.model.pojo.User;
import stu.aistar.service.UserService;
import stu.aistar.util.CodeUtils;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 通过手机号查询用户是否存在
     * @param phone
     * @return
     */
    @Override
    public User findUserByPhone(String phone) {
        return userMapper.findUserByPhone(phone);
    }

    @Override
    public User login(String phone, String password) {
        return userMapper.findUserByPhoneAndPassword(phone,password);
    }

    /**
     * 注册用户
     * @param phone
     * @param password
     */
    @Override
    public User registUser(String phone, String password) {
        User user = new User();
        user.setPhone(phone);
        user.setPassword(password);
        user.setPower(0);
        return userMapper.save(user);
    }

    /**
     * 发送验证码，并返回
     * @param phoneNumber
     * @param code_length
     * @param map
     * @return
     */
    @Override
    public String sendVerifyCode(String phoneNumber, Integer code_length, Map<String, String> map) {
        return CodeUtils.sendVerifyCode(phoneNumber, code_length, map);
    }

    /**
     * 获取所有用户信息
     * @return
     */
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }



    @Override
    public PageInfo<User> findUsersByPage(String page, String pageSize, String username, String phone, String name) {
        PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(pageSize));
        List<User> users = userMapper.findUsersByConditions(username,phone,name);
        PageInfo<User> pageInfo = new PageInfo<User>(users);
        System.out.println(pageInfo);
        return pageInfo;
    }

    @Override
    public int delUserById(Integer id) {
        return userMapper.delteUserById(id);
    }

    @Override
    public int updateUser(User user){
        return userMapper.updateUser(user);
    }

    @Override
    public int insertUserByImport(User user) {
        return userMapper.insertUser(user);
    }
}
